import { userlist, getCount } from '../../../request/api'
const state = {
    userlist: [],
    size: 2,
    page: 1,
    count: 0
}

const getters = {
    getUser(state) {
        return state.userlist;
    },
    //封装总条数方法
    getCount(state) {
        return state.count
    }
}

const mutations = {
    REQ_LIST(state, payLoad) {
        state.userlist = payLoad
    },
    REQ_COUNT(state,payLoad){
        state.count = payLoad
    },
    REQ_PAGE(state,payLoad){
        state.page = payLoad
    }
}

const actions = {
    getMangerActions(context) {
        userlist({
            size: context.state.size,
            page: context.state.page
        })
            .then(res => {
                if (res.code == 200) {
                    let list = res.list?res.list:[];
                    if(context.state.page!=1 && list.length==0){
                        //修改page
                        // let page = context.state.page-1;
                        // context.commit('REQ_PAGE',page)
                        // //重新调用接口
                        // context.dispatch('getMangerActions')
                        context.dispatch('changePageActions',context.state.page-1)
                    }
                    context.commit('REQ_LIST', res.list?res.list : [])
                }
            })
    },
    getCountActions({ commit }) {
        getCount().then(res => {
            console.log(res,'总条数')
            if (res.code == 200) {               
                commit('REQ_COUNT',res.list[0].total)
            }
        })
    },
    changePageActions(context,payLoad){
        context.commit('REQ_PAGE',payLoad)
        context.dispatch('getMangerActions')
    }

}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}